package cn.kgc.crowdfundingadmin03.service;

import cn.kgc.crowdfundingadmin03.mapper.RoleMapper;
import cn.kgc.crowdfundingadmin03.pojo.Role;
import cn.kgc.crowdfundingadmin03.pojo.RoleExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-02-25 14:59
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {


    @Resource
    RoleMapper roleMapper;

    @Override
    public PageInfo<Role> queryForKeywordWithPage(Integer pageNum, Integer pageSize, String keyword) {
        //进行分页的查询
        //1.开启分页功能
        PageHelper.startPage(pageNum, pageSize);
        //2.执行查询
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        //跟据名字查询  模糊
        criteria.andNameLike("%" + keyword + "%");
        List<Role> list = roleMapper.selectByExample(example);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Role> getRoleListByIdList(List<Integer> roleIdList) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(roleIdList);
        List<Role> roles = roleMapper.selectByExample(example);
        return roles;
    }

    @Override
    public List<Role> getAssignedRoleList(Integer adminId) {
        return roleMapper.getAssignedRoleList(adminId);
    }

    @Override
    public List<Role> getUnAssignedRoleList(Integer adminId) {
        return roleMapper.getUnAssignedRoleList(adminId);
    }

    @Override
    public void updateRelationship(Integer adminId, List<Integer> roleIdList) {
        roleMapper.deletAdminRelationRole(adminId);

        if (roleIdList != null) {
            roleMapper.insertAdminRelationRole(adminId, roleIdList);
        }
    }

    @Override
    public void batchRemove(List<Integer> roleIdList) {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(roleIdList);
        roleMapper.deleteByExample(example);
    }

    @Override
    public void saveRole(String roleName) {
        roleMapper.insert(new Role(null, roleName));
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }
}
